package dao

import (
	"errors"
	"fmt"
	"go_payment/app/param_model"
	"go_payment/model"
)

func GetChannelByProduct(product *model.ProductUser, trade *param_model.Trade) (int, error) {
	if product.Polling == 0 {
		if product.Channel == 0 {
			return 0, errors.New(fmt.Sprintf("商户%d的%d单独渠道未正确配置", trade.MerchantId, trade.BankCode))
		}
		return product.Channel, nil
	}
	//todo 多通道轮询未写
	return 0, nil
}

func GetChannelCode(channelId int) string {
	sql := "SELECT `code` FROM `pay_channel` WHERE `id`=? AND `status`=1 LIMIT 1"
	stmt, err := db.Prepare(sql)
	defer stmt.Close()
	if err != nil {
		panic(err)
	}
	row := stmt.QueryRow(channelId)
	var code string
	row.Scan(&code)
	return code
}

func GetChannelInfoById(id int) model.Channel {
	sql := "SELECT `id`,`bankid`,`code`,`title`,`defaultrate` FROM pay_channel WHERE `id`=? LIMIT 1"
	stmt, err := db.Prepare(sql)
	defer stmt.Close()
	if err != nil {
		panic(err)
	}
	row := stmt.QueryRow(id)
	var channel model.Channel
	row.Scan(&channel.Id, &channel.BankId, &channel.Code, &channel.Title, &channel.DefaultRate)
	return channel
}
